import gtk
import PGN
from boardwindow import BoardWindow

class GameList(gtk.TreeView):
	def __init__(self, window):
		self.parent_window = window
		self.games = []
	
		gtk.TreeView.__init__(self)
		
		self.connect("row-activated", self.on_row_activated)
		
		self.set_rules_hint(True)
		self.set_enable_search(False)
		
		self.store = gtk.ListStore(int, str)
		self.set_model(self.store)
		
		column_game = gtk.TreeViewColumn('Game Description')
		
		self.append_column(column_game)
		
		cell = gtk.CellRendererText()
		column_game.pack_start(cell, True)
		column_game.add_attribute(cell, 'markup', 1)
		
		
	def load(self, filename):
		self.store.clear()
		
		settings = gtk.settings_get_default()
		font_style = gtk.rc_get_style_by_paths(settings, "SmallSystemFont", None, None)
		font_desc = font_style.font_desc
		color_style = gtk.rc_get_style_by_paths(settings, 'GtkButton', 'osso-logical-colors', gtk.Button)
		color = color_style.lookup_color("SecondaryTextColor")
		
		for game in PGN.parse(filename):
			self.games.append(game)
			
			gamestring = '%s <span font_desc="%s" foreground="%s">(%s)</span>' % (game.keys['White'], font_desc.to_string(), color.to_string(), game.keys['WhiteElo'])
			gamestring += '  %s <span font_desc="%s" foreground="%s">(%s)</span>' % (game.keys['Black'], font_desc.to_string(), color.to_string(), game.keys['BlackElo'])
			gamestring += '\n<span font_desc="%s" foreground="%s">' % (font_desc.to_string(), color.to_string())
			if game.keys['Event'] != "": gamestring += '%s - ' % game.keys['Event']
			if game.keys['Date'] != "": gamestring += '%s - ' % game.keys['Date']
			gamestring += '%s' % game.keys['Result']
			gamestring += '</span>'
			                   
			self.store.append([game.index, gamestring])
		
		self.set_cursor(0)
		self.columns_autosize()
		
	def on_row_activated(self, widget, path, col):
		boardwindow = BoardWindow()
		game = self.games[self.store[path][0]]
		boardwindow.set_title (game.keys['White'] + " - " + game.keys['Black'])
		boardwindow.open_game(game)
		
		
